<template>
  <div>
    <!-- 上传组件标题 -->
    <div class="block">{{ uploadTitle }}</div>
    <!-- 上传控件 -->
    <!-- action="/api/upload" -->
    <!-- :headers="headers" -->
    <el-upload
      class="avatar-uploader"
      action="#"
      :http-request="UploadImage"
      :show-file-list="false"
      :auto-upload="false"
      :on-change="fileChange"
      accept="image/jpeg,image/png,image/jpg"
      ref="uploadBox"
    >
      <img v-if="value" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
// import { server_URL } from "@/urlConfig.js";
import OSS from "ali-oss";
export default {
  props: ["uploadTitle", "value"],
  data() {
    return {
      fileList: [],
      file: "",
      // imageUrl: "",
    };
  },
  computed: {
    imageUrl() {
      if (this.value) {
        // return server_URL + this.value;
        return this.value;
      }
    },
    headers() {
      return {
        // Authorization: "Bearer " + localStorage.getItem("adminToken"), // 从本地获取 token，添加到 header 里面
      };
    },
  },
  methods: {
    UploadImage(param) {},
    typeJudge(file) {
      const isIMAGE = file.type === "image/jpeg" || "image/jpg" || "image/png";
      const isLt1M = file.size / 1024 / 1024 < 4;
      if (!isIMAGE) {
        this.$message.error("上传文件只能是图片格式!");
      }
      if (!isLt1M) {
        this.$message.error("上传文件大小不能超过 4MB!");
      }
      // console.log(isIMAGE && isLt1M, "boolean");
      return isIMAGE && isLt1M;
    },
    fileChange(file) {
      //
      if (this.typeJudge(file.raw)) {
        console.log(this.imageUrl);
        this.$refs.uploadBox.clearFiles(); //清除文件对象
        this.file = file.raw;
        this.handleUpload(file.raw);
      }
    },
    async handleUpload(file) {
      const client = new OSS({
        accessKeyId: "LTAI5tG5NbpvLQxhM5W1B9ys",
        accessKeySecret: "vJGnTSThKsLiYPRS9v6jyk3v99Mp8u",
        bucket: "join-blog",
        region: "oss-cn-beijing",
      });
      try {
        const ossRes = await client.put(
          new Date().getTime() + "-" + file.name,
          file
        );

        if (ossRes.res.status === 200) {
          console.log(ossRes.res.requestUrls);
          // this.imageUrl = ossRes.res.requestUrls;
          this.$emit("input", ossRes.res.requestUrls[0]);
          // return ossRes.res.requestUrls;
        } else {
          console.error("oss上传失败");
        }
      } catch (e) {
        console.log(
          "%c 🍠 e: ",
          "font-size:20px;background-color: #2EAFB0;color:#fff;",
          e
        );
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.block {
  margin: 15px 0;
  font-weight: 100;
}
.avatar-uploader .el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>